#ifndef AVPLUGIN_H
#define AVPLUGIN_H

#include <QtGui/QImageIOPlugin>
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include "avhandler.h"

class AVPlugin : public QImageIOPlugin {
    Q_OBJECT

public:
    AVPlugin(QObject *parent = 0) : QImageIOPlugin(parent) {
        //av_register_all();
        avcodec_register_all();
        avdevice_register_all();
    }

    QStringList keys() const {
        static QStringList keys;
        if (keys.isEmpty()) {
            AVInputFormat *fmt = NULL;
            while ((fmt = av_iformat_next(fmt))) {
                keys.append(QString(fmt->name).split(','));
            }
        }
        return keys;
    }

    Capabilities capabilities(QIODevice *, const QByteArray &format) const
    {
        if(keys().contains(format, Qt::CaseInsensitive)) {
            return CanRead|CanReadIncremental;
        }
        return 0;
    }

    AVHandler* create(QIODevice *device, const QByteArray &format) const
    {
        if(keys().contains(format, Qt::CaseInsensitive)) {
            AVHandler* handler = new AVHandler;
            handler->setDevice(device);
            handler->setFormat(format);
            if (QFile *file = qobject_cast<QFile*>(device))
                handler->setFileName(file->fileName());
            else
                handler->setFileName(device->property("fileName").toString());

            return handler;
        }
        return 0;
    }
};
Q_EXPORT_PLUGIN2(libav, AVPlugin)

#endif // AVPLUGIN_H
